#include "examinitionthread.h"

ExaminitionThread::ExaminitionThread(QObject *parent) :
    QThread(parent)
{
    qRegisterMetaType<SystemState>("SystemState");
    m_closeFlag = true;

    m_cpuIsOverLim = false;
    m_hardDiskIsOverLim = false;
    m_memIsOverLim = false;

    connect(this, SIGNAL(finished()), this, SLOT(quit()));
}

void ExaminitionThread::close()
{
    m_closeFlag = false;
    emit finished();
}

void ExaminitionThread::run()
{
//    QTimer *checkTimer = new QTimer;

//    checkTimer->setSingleShot(false);
//    checkTimer->start(CHECKTIMES);

    while(m_closeFlag)
    {
        checkSysState();
        msleep(1000 * 2 * m_closeFlag);
    }

//    connect(checkTimer, SIGNAL(timeout()), this, SLOT(timeToCheckSys()));
//    timeToCheckSys();
//    this->exec();
}


void ExaminitionThread::timeToCheckSys()
{
    SystemState state = m_systemExam.getSystemState();
    if (m_testSysState.cpuOccRate - state.cpuOccRate > CHANGEDEGREE || m_testSysState.cpuOccRate - state.cpuOccRate < (-1 * CHANGEDEGREE) ||
            m_testSysState.memOccRate - state.memOccRate > CHANGEDEGREE || m_testSysState.memOccRate - state.memOccRate < (-1 * CHANGEDEGREE) ||
            m_testSysState.hardDiskOccRate - state.hardDiskOccRate > CHANGEDEGREE || m_testSysState.hardDiskOccRate - state.hardDiskOccRate < (-1 * CHANGEDEGREE) )
    {
        m_testSysState = state;
        emit systemState(m_testSysState);
    }
    m_sysState = state;

}

inline void ExaminitionThread::checkSysState()
{
//    qDebug() << "checkSysState=================";
    SystemState state = m_systemExam.getSystemState();

    if (m_testSysState.cpuOccRate - state.cpuOccRate > CHANGEDEGREE || m_testSysState.cpuOccRate - state.cpuOccRate < (-1 * CHANGEDEGREE) ||
            m_testSysState.memOccRate - state.memOccRate > CHANGEDEGREE || m_testSysState.memOccRate - state.memOccRate < (-1 * CHANGEDEGREE) ||
            m_testSysState.hardDiskOccRate - state.hardDiskOccRate > CHANGEDEGREE || m_testSysState.hardDiskOccRate - state.hardDiskOccRate < (-1 * CHANGEDEGREE) )
    {
        m_testSysState = state;
        emit systemState(m_testSysState);
    }
    m_sysState = state;

    //如果当前使用率超过安全界线,那么将出现警告
    if (m_sysState.cpuOccRate >= m_configClass.getCputLimmit())
    {
        if (m_cpuIsOverLim == false)
        {
            m_cpuIsOverLim = true;
            logger()->warn(tr("CPU utilization rate is %1\%;Has exceeded safety limit!!!\n").arg(m_sysState.cpuOccRate));
        }
    } else {
        if (m_cpuIsOverLim == true)
        {
            m_cpuIsOverLim = false;
            logger()->info(tr("CPU utilization rate is %1\%;Has been restored security\n").arg(m_sysState.cpuOccRate));

        }
    }

    if (m_sysState.memOccRate >= m_configClass.getMemoryLimmit())
    {
        if (m_memIsOverLim == false)
        {
            m_memIsOverLim = true;
            logger()->warn(tr("Memory utilization rate is %1\%;Has exceeded safety limit!!!\n").arg(m_sysState.memOccRate));

        }
    } else {
        if (m_memIsOverLim == true)
        {
            m_memIsOverLim = false;
            logger()->info(tr("Memory utilization rate is %1\%;Has been restored security\n").arg(m_sysState.memOccRate));
        }
    }

    if (m_sysState.hardDiskOccRate >= m_configClass.getHardDiskLimmit())
    {
        if (m_hardDiskIsOverLim == false)
        {
            m_hardDiskIsOverLim = true;
            logger()->warn(tr("HardDisk utilization rate is %1\%;Has exceeded safety limit!!!\n").arg(m_sysState.hardDiskOccRate));
        }
    } else {
        if (m_hardDiskIsOverLim == true)
        {
            m_hardDiskIsOverLim = false;
            logger()->info(tr("HardDisk utilization rate is %1\%;Has been restored security\n").arg(m_sysState.hardDiskOccRate));
        }
    }
}

void ExaminitionThread::appCheckSysState()
{
    emit systemState(m_sysState);
}

